﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;

public partial class BBS_BbsManage_SectionEdit : Baimasoft.Web.UI.PageFormTop 
{
    CMods MyCM = new CMods();
    PMods MyPM = new PMods();
    public string Log = "";

    protected override void InitForm()
    {        
        if (!Page.IsPostBack)
        {
            GroupNameDrpBind();

            try
            {
                string sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
                if (sEditStatus == "2")
                {
                    string sID = this.Request.QueryString["ID"].ToString().Trim();
                    SqlDataReader MySDR =  MyCM .ReadCModsInfo ("a.SectionID =" + sID);
                    if (MySDR.Read())
                    {
                        GroupNameDrp.Text = MySDR["GroupID"].ToString().Trim();
                        SectionName.Text = MySDR["SectionName"].ToString().Trim();
                        Ord.Text = MySDR["Ord"].ToString().Trim();
                        Log = MySDR["HeadLog"].ToString().Trim();

                        Master.Text = MySDR["Master"].ToString().Trim();
                        Descr.Text = MySDR["Descr"].ToString().Trim();
                        Visiable.Text = MySDR["Visiable"].ToString().Trim();
                    }
                }
            }
            catch
            {
            }
        }
    }

    public void GroupNameDrpBind()
    {
        GroupNameDrp.DataSource = MyPM.GetPModsInfo("");
        GroupNameDrp.DataBind();
        GroupNameDrp.Items.Insert(0, "请选择");
        GroupNameDrp.Items[0].Value = "0";
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string sGroupID = GroupNameDrp.SelectedValue.ToString().Trim();
        string sSectionName = SectionName.Text.ToString().Trim();

        string sOrd = Ord.Text.ToString().Trim();
        string sHeadLog = HeadLog.Value.ToString().Trim();

        string sMaster = Master.Text.ToString().Trim();

        string sDescr = Descr.Text.ToString().Trim();
        string sVisiable = Visiable.SelectedValue.ToString().Trim();

        string Number = @"^[0-9]+$";

        if (!Regex.IsMatch(sOrd, Number) && sOrd != "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('排序号只能是数字！')</script>");
            return;
        }

        if (sGroupID == "0")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择讨论组名称！')</script>");
            return;
        }

        if (sSectionName == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('讨论区名称不能为空！')</script>");
            return;
        }

        if (sMaster == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择版主名称！')</script>");
            return;
        }
        else
        {
            //上传附件
            if (sHeadLog != "")
            {//有附件上传时
                int Len = HeadLog.PostedFile.ContentLength;
                if (Len < 20480)
                {
                    string PicType = sHeadLog.Substring(sHeadLog.LastIndexOf(".") + 1);
                    if (PicType.ToUpper() == "JPG" || PicType.ToUpper() == "GIF" || PicType.ToUpper() == "BMP")
                    {
                        string FileName = sHeadLog.Substring(sHeadLog.LastIndexOf("\\") + 1);
                        string FileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString();
                        sHeadLog = FileTime + FileName;
                        Request.Files[0].SaveAs(Server.MapPath("HeadLog") + "\\" + sHeadLog);
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择JPG、GIF、BMP格式的图片上传！')</script>");
                        return;
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传的图片要小于20K！')</script>");
                    return;
                }
            }
            else
            {
                sHeadLog = "SectionPic.GIF";
            }
        }

        if (sDescr.Length > 100)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('描述内容最多限100个字！')</script>");
            return;
        }

        if (this.EditStatus == EditStatus.Add)
        {
            int iback = MyCM.AddCMods(sGroupID, sSectionName, sMaster, sDescr, sVisiable, DateTime.Now, sOrd, sHeadLog);
            if (iback > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');window.location.href='SectionList.aspx';</script>");
                return;
            }
        }
        else
        {
            DateTime UpDt = DateTime.Now;
            int iback = MyCM.UpdateCMods(sGroupID, sSectionName, sMaster, sDescr, sVisiable, DateTime.Now, sOrd, sHeadLog, this.Request.QueryString["ID"].ToString().Trim());
            if (iback > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.location.href='SectionList.aspx';</script>");
                return;
            }
        }
    }
}
